﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Transcend.FileUploader.Entity
{
	/// <summary>
	/// dspage描述类
	/// </summary>
	public class DocPage
	{

		/// <summary>
		/// 文档编号
		/// </summary>
		public decimal Id { get; set; }

		/// <summary>
		/// 文档分类ID
		/// </summary>
		public decimal ItemId { get; set; }

		/// <summary>
		/// 文档页码
		/// </summary>
		public int PageNo { get; set; }

		/// <summary>
		/// 状态标志
		/// </summary>
		public int Status { get; set; }

		private long size = 0;
		//文档大小
		public long Size
		{
			get
			{
				if (size > 0)
				{
					return size;
				}

				if (Data != null && Data.Length > 0)
				{
					return Data.Length;
				}

				if (!string.IsNullOrEmpty(LocalPath))
				{
					return (new System.IO.FileInfo(LocalPath)).Length;
				}

				return 0;
			}
			set
			{
				size = value;
			}
		}

		/// <summary>
		/// 页数
		/// </summary>
		public int PageNum { get; set; }

		/// <summary>
		/// ...
		/// </summary>
		public string Keyword { get; set; }

		/// <summary>
		/// 新增时间 默认为 DateTime.Now
		/// </summary>
		public DateTime CreateDate { get; set; }

		/// <summary>
		/// 文档文件名
		/// </summary>
		public string RawFileName { get; set; }

		/// <summary>
		/// 文件扩展名
		/// </summary>
		public string Extension { get; set; }

		/// <summary>
		/// 更新时间 默认为 DateTime.Now
		/// </summary>
		public DateTime UpdateTime { get; set; }

		/// <summary>
		/// 操作ID
		/// </summary>
		public decimal UserId { get; set; }


		/// <summary>
		/// 设置/获取 文档数据内容
		/// </summary>
		public byte[] Data { get; set; }

		/// <summary>
		/// 所属分类
		/// </summary>
		public DocCategory Category { get; set; }

		/// <summary>
		/// 访问路径，用于调取非数据库存储文档信息
		/// </summary>
		public string Uri { get; set; }

		/// <summary>
		/// 用于保存文件上传时的本地路径
		/// </summary>
		public string LocalPath { get; set; }

		/// <summary>
		/// 取该页下的所有子文档页
		/// </summary>
		public List<DocPage> SubPages { get; set; }

		/// <summary>
		/// 添加子文档页
		/// </summary>
		public DocPage Add(DocPage page)
		{
			SubPages.Add(page);
			return page;
		}

		public int Width { get; set; }

		public int Height { get; set; }

		/// <summary>
		/// 添加子文档页
		/// </summary>
		public DocPage Add(int pageno, string rawfilename)
		{
			DocPage page = new DocPage();
			page.PageNo = pageno;
			page.RawFileName = rawfilename;
			page.Id = this.Id;
			page.ItemId = this.ItemId;
			return Add(page);
		}

		public DocPage()
			: this(0, 0, 0, string.Empty, 0)
		{
		}

		public DocPage(decimal imgno, decimal itemid, int pageno, string rawfilename, decimal userid)
			: this(imgno, itemid, pageno, rawfilename, userid, null)
		{

		}

		public DocPage(decimal imgno, decimal itemid, int pageno, string rawfilename, decimal userid,  byte[] data)
			: this(imgno, itemid, pageno, 1, string.Empty, DateTime.Now, rawfilename, DateTime.Now, userid,  data)
		{

		}

		public DocPage(decimal imgno, decimal itemid, int pageno, int pagenum, string keyword, DateTime createdate, string rawfilename, DateTime updatetime, decimal userid, byte[] data)
		{
			Id = imgno;
			ItemId = itemid;
			PageNo = pageno;
			Size = (data == null ? 0 : data.Length);
			PageNum = pagenum;
			Keyword = keyword;
			CreateDate = createdate;
			RawFileName = rawfilename;
			UpdateTime = updatetime;
			UserId = userid;
			Data = data;
			Width = 0;
			Height = 0;
			SubPages = new List<DocPage>();
			Status = 1;
		}

	}
}
